package GUI.TabPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import Config.GeneralsConfig;
import FileStatus.Property;
import Logic.TaskMaintain;

public class Generals extends JPanel {

	private static final long serialVersionUID = -8398522571456605734L;
	private JButton save;
	private JLabel username;
	private JLabel password;
	private JLabel ip;
	private JLabel port;
	private JLabel database;
	private JTextField usernameText;
	private JTextField passwordText;
	private JTextField ipText;
	private JTextField portText;
	private JTextField databaseText;
	private JPanel panel;
	private String Mac_ID = null;

	public Generals() {
		initPanel();
	}

	private void initPanel() {
		save = new JButton("Save");
		username = new JLabel("username");
		password = new JLabel("password");
		ip = new JLabel("ip");
		port = new JLabel("port");
		database = new JLabel("database");

		usernameText = new JTextField();
		passwordText = new JTextField();
		ipText = new JTextField();
		portText = new JTextField();
		databaseText = new JTextField();

		panel = new JPanel();

		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (usernameText.getText().isEmpty()
						|| passwordText.getText().isEmpty()
						|| ipText.getText().isEmpty()
						|| portText.getText().isEmpty()
						|| databaseText.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Please complete all text");
				} else {
					if (null != Mac_ID) {
						GeneralsConfig.stringDataBaseUsername = usernameText
								.getText();
						GeneralsConfig.stringDataBasePassword = passwordText
								.getText();
						GeneralsConfig.stringDataBaseUrl = "jdbc$oracle$thin$@"
								+ ipText.getText() + "$" + portText.getText()
								+ "$" + databaseText.getText();

						try {
							TaskMaintain.writeCON(Mac_ID);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

						System.out.println(GeneralsConfig.stringDataBaseUrl);
						// ComWork.writeConfig(Mac_ID, usernameText.getText(),
						// passwordText.getText(),
						// ipText.getText(), portText.getText(),
						// databaseText.getText());
					}
				}
			}

		});

		GroupLayout layout = new GroupLayout(panel);
		panel.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(36, 36, 36)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addComponent(username)
												.addComponent(password)
												.addComponent(database))
								.addGap(20, 20, 20)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						usernameText,
																						120,
																						120,
																						120)
																				.addComponent(
																						passwordText,
																						120,
																						120,
																						120))
																// .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
																.addGap(80, 80,
																		80)
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										ip)
																								.addGap(38,
																										38,
																										38)
																								.addComponent(
																										ipText,
																										120,
																										120,
																										120))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										port)
																								.addGap(20,
																										20,
																										20)
																								.addGroup(
																										layout.createParallelGroup(
																												GroupLayout.Alignment.LEADING)
																												.addComponent(
																														save)
																												.addComponent(
																														portText,
																														120,
																														120,
																														120)))))
												.addComponent(databaseText,
														120, 120, 120))
								.addContainerGap(45, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(42, 42, 42)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(username)
										.addComponent(ip)
										.addComponent(usernameText,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(ipText,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(42, 42, 42)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(password)
										.addComponent(port)
										.addComponent(passwordText,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(portText,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(42, 42, 42)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(database)
										.addComponent(databaseText,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.RELATED, 84,
								Short.MAX_VALUE).addComponent(save)
						.addGap(34, 34, 34)));

		GroupLayout _layout = new GroupLayout(this);
		this.setLayout(_layout);
		_layout.setHorizontalGroup(_layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
				Short.MAX_VALUE));
		_layout.setVerticalGroup(_layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
				Short.MAX_VALUE));

	}

	public void Refresh(Object SelectObject) {
		Mac_ID = (String) SelectObject;
		try {
			Property.readProperty(Mac_ID);
		} catch (ClassNotFoundException | NoSuchFieldException
				| SecurityException | IllegalArgumentException
				| IllegalAccessException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error!!!");
		}
		System.out.println(Mac_ID);
		usernameText.setText(GeneralsConfig.stringDataBaseUsername);
		passwordText.setText(GeneralsConfig.stringDataBasePassword);
		// GeneralsConfig.stringDataBaseUrl =
		// "jdbc$oracle$thin$@192.168.56.101$1521$orcl";
		if (GeneralsConfig.stringDataBaseUrl != "") {
			String[] tmp = GeneralsConfig.stringDataBaseUrl.replace("$", ":")
					.split(":");
			portText.setText(tmp[4]);
			ipText.setText(tmp[3].replace("@", ""));
			databaseText.setText(tmp[5]);
		} else {
			portText.setText("");
			ipText.setText("");
			databaseText.setText("");
		}
	}
}
